import * as THREE from "three"

/**
 * @author 杨俊
 * texture memory manager
 * use common material and texture
 */

/** @type {Map<string,THREE.Texture>} 贴图 */
const textureMap = new Map();

const textureLoader = new THREE.TextureLoader();

/**
 * 加载纹理，并设置纹理色彩空间
 * @param {string} url
 * @param {THREE.ColorSpace} colorSpace
 * @returns
 */
export function loadTexture(url, colorSpace = THREE.SRGBColorSpace) {

    if (textureMap.has(url)) {

        return textureMap.get(url);

    } else {

        const texture = textureLoader.load(url);

        texture.colorSpace = colorSpace;

        textureMap.set(url, texture);

        return texture;

    }

}

export const loadEnvironmentTexture = (url) => {

    const texture = loadTexture(url);

    texture.mapping = THREE.EquirectangularReflectionMapping;

    return texture;

};
